package com.sdehualu.utils


import org.apache.spark.sql.{Row, RowFactory}
import ucar.ma2.ArrayFloat
import ucar.nc2.Variable

/**
 * @Author: 吴敬超
 * @Date: 2021/7/23 17:56
 */
object Var2Row {
  def swithlistallstring(parnum: Int, vards: Int, variable: Variable, dilist: List[Array[String]], firflag: Boolean): List[Row] = {
    var rowList: List[Row] = Nil

    try vards match { //一维的 var
      case 1 =>
        val floatvar1 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Float]]
        //        rowList.add(RowFactory.create(floatvar1(parnum)))
        rowList = rowList :+ Row(floatvar1(parnum))
        //        Variable.read.get1DJavaArray(classOf[String]).asInstanceOf[Array[String]]
        //        Variable.read.get1DJavaArray(classOf[String]).asInstanceOf[Array[Array[String]]]
        return rowList
      case 2 =>
        val floatvar2 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Float]]]
        //
        // dilist.get(0);
        // dilist.get(1);
        //2维的var 且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar2(parnum).length) {

            rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), floatvar2(parnum)(i))


          }
          //2维的var 且根据2 dimension 分区
        }
        else {

          rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), floatvar2(0)(parnum))
          //            rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), floatvar2(0)(parnum)))
        }
        return rowList
      case 3 =>
        val floatvar3 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Float]]]]
        //3维数据，且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar3(parnum).length) {
            for (i2 <- 0 until floatvar3(parnum)(i).length) {
              //              rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), floatvar3(parnum)(i)(i2)))
              rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), floatvar3(parnum)(i)(i2))


            }
          }
          // 3维的var 且根据2 dimension 分区
        }
        else for (i <- 0 until floatvar3(0)(parnum).length) {
          //          rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), floatvar3(0)(parnum)(i)))
          rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), floatvar3(0)(parnum)(i))

        }
        return rowList
      case 4 =>
        val floatvar4 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Float]]]]]
        //4维数据，且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar4(parnum).length) {
            for (i2 <- 0 until floatvar4(parnum)(i).length) {
              for (i3 <- 0 until floatvar4(parnum)(i)(i2).length) {
                //                rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), floatvar4(parnum)(i)(i2)(i3)))

                rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), floatvar4(parnum)(i)(i2)(i3))
              }
            }
          }
          //     4维的var 且根据2 dimension 分区
        }
        else for (i <- 0 until floatvar4(0)(parnum).length) {
          for (i2 <- 0 until floatvar4(0)(parnum)(i).length) {
            //            rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), floatvar4(0)(parnum)(i)(i2)))

            rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), floatvar4(0)(parnum)(i)(i2))

          }
        }
        return rowList
      case 5 =>
        val floatvar5 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Array[Float]]]]]]
        //且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar5(parnum).length) {
            for (i2 <- 0 until floatvar5(parnum)(i).length) {
              for (i3 <- 0 until floatvar5(parnum)(i)(i2).length) {
                for (i4 <- 0 until floatvar5(parnum)(i)(i2)(i3).length) {
                  //                  rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), dilist.get(4)(i4), floatvar5(parnum)(i)(i2)(i3)(i4)))

                  rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), dilist(4)(i4), floatvar5(parnum)(i)(i2)(i3)(i4))


                }
              }
            }
          }
          //    根据2 dimension 分区
        }
        else for (i <- 0 until floatvar5(0)(parnum).length) {
          for (i2 <- 0 until floatvar5(0)(parnum)(i).length) {
            for (i3 <- 0 until floatvar5(0)(parnum)(i)(i2).length) {
              //              rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), dilist.get(4)(i3), floatvar5(0)(parnum)(i)(i2)(i3)))

              rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), dilist(4)(i3), floatvar5(0)(parnum)(i)(i2)(i3))


            }
          }
        }
        return rowList
      case 6 =>
        val floatvar6 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Array[Array[Float]]]]]]]
        //        val floatvar6 = variable.read.copyToNDJavaArray.asInstanceOf[ArrayFloat.]

        if (firflag) {
          for (i <- 0 until floatvar6(parnum).length) {
            for (i2 <- 0 until floatvar6(parnum)(i).length) {
              for (i3 <- 0 until floatvar6(parnum)(i)(i2).length) {
                for (i4 <- 0 until floatvar6(parnum)(i)(i2)(i3).length) {
                  for (i5 <- 0 until floatvar6(parnum)(i)(i2)(i3).length) {
                    //                    rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), dilist.get(4)(i4), dilist.get(5)(i5), floatvar6(parnum)(i)(i2)(i3)(i4)(i5)))

                    rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), dilist(4)(i4), dilist(5)(i5), floatvar6(parnum)(i)(i2)(i3)(i4)(i5))

                  }
                }
              }
            }
          }
          //根据2 dimension 分区
        }
        else for (i <- 0 until floatvar6(0)(parnum).length) {
          for (i2 <- 0 until floatvar6(0)(parnum)(i).length) {
            for (i3 <- 0 until floatvar6(0)(parnum)(i)(i2).length) {
              for (i4 <- 0 until floatvar6(0)(parnum)(i)(i2)(i3).length) {
                //                rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), dilist.get(4)(i3), dilist.get(5)(i4), floatvar6(0)(parnum)(i)(i2)(i3)(i4)))

                rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), dilist(4)(i3), dilist(5)(i4), floatvar6(0)(parnum)(i)(i2)(i3)(i4))


              }
            }
          }
        }
        return rowList
      // case 7:
      //     float floatvar7[][][][][][][] = (float[][][][][][][]) variable.read().copyToNDJavaArray();
      //     // return new ArrayDouble.D6(index, storage);
      //     return rowList;
      case _ =>
        return null
    }
    catch {
      case e: Exception =>
        e.printStackTrace()
    }
    null
  }


  def swithlist(parnum: Int, vards: Int, variable: Variable, dilist: List[Array[String]], firflag: Boolean): List[Row] = {
    var rowList: List[Row] = Nil

    try vards match { //一维的 var
      case 1 =>
        val floatvar1 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Float]]
        //        rowList.add(RowFactory.create(floatvar1(parnum)))
        rowList = rowList :+ Row(floatvar1(parnum))
        //        Variable.read.get1DJavaArray(classOf[String]).asInstanceOf[Array[String]]
        //        Variable.read.get1DJavaArray(classOf[String]).asInstanceOf[Array[Array[String]]]
        return rowList
      case 2 =>
        val floatvar2 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Float]]]
        //
        // dilist.get(0);
        // dilist.get(1);
        //2维的var 且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar2(parnum).length) {

            rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), floatvar2(parnum)(i))


          }
          //2维的var 且根据2 dimension 分区
        }
        else {

          rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), floatvar2(0)(parnum))
          //            rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), floatvar2(0)(parnum)))
        }
        return rowList
      case 3 =>
        val floatvar3 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Float]]]]
        //3维数据，且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar3(parnum).length) {
            for (i2 <- 0 until floatvar3(parnum)(i).length) {
              //              rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), floatvar3(parnum)(i)(i2)))
              rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), floatvar3(parnum)(i)(i2))


            }
          }
          // 3维的var 且根据2 dimension 分区
        }
        else for (i <- 0 until floatvar3(0)(parnum).length) {
          //          rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), floatvar3(0)(parnum)(i)))
          rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), floatvar3(0)(parnum)(i))

        }
        return rowList
      case 4 =>
        val floatvar4 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Float]]]]]
        //4维数据，且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar4(parnum).length) {
            for (i2 <- 0 until floatvar4(parnum)(i).length) {
              for (i3 <- 0 until floatvar4(parnum)(i)(i2).length) {
                //                rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), floatvar4(parnum)(i)(i2)(i3)))

                rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), floatvar4(parnum)(i)(i2)(i3))
              }
            }
          }
          //     4维的var 且根据2 dimension 分区
        }
        else for (i <- 0 until floatvar4(0)(parnum).length) {
          for (i2 <- 0 until floatvar4(0)(parnum)(i).length) {
            //            rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), floatvar4(0)(parnum)(i)(i2)))

            rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), floatvar4(0)(parnum)(i)(i2))

          }
        }
        return rowList
      case 5 =>
        val floatvar5 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Array[Float]]]]]]
        //且根据1 dimension 分区
        if (firflag) {
          for (i <- 0 until floatvar5(parnum).length) {
            for (i2 <- 0 until floatvar5(parnum)(i).length) {
              for (i3 <- 0 until floatvar5(parnum)(i)(i2).length) {
                for (i4 <- 0 until floatvar5(parnum)(i)(i2)(i3).length) {
                  //                  rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), dilist.get(4)(i4), floatvar5(parnum)(i)(i2)(i3)(i4)))

                  rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), dilist(4)(i4), floatvar5(parnum)(i)(i2)(i3)(i4))


                }
              }
            }
          }
          //    根据2 dimension 分区
        }
        else for (i <- 0 until floatvar5(0)(parnum).length) {
          for (i2 <- 0 until floatvar5(0)(parnum)(i).length) {
            for (i3 <- 0 until floatvar5(0)(parnum)(i)(i2).length) {
              //              rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), dilist.get(4)(i3), floatvar5(0)(parnum)(i)(i2)(i3)))

              rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), dilist(4)(i3), floatvar5(0)(parnum)(i)(i2)(i3))


            }
          }
        }
        return rowList
      case 6 =>
        val floatvar6 = variable.read.copyToNDJavaArray.asInstanceOf[Array[Array[Array[Array[Array[Array[Float]]]]]]]
        //        val floatvar6 = variable.read.copyToNDJavaArray.asInstanceOf[ArrayFloat.]

        if (firflag) {
          for (i <- 0 until floatvar6(parnum).length) {
            for (i2 <- 0 until floatvar6(parnum)(i).length) {
              for (i3 <- 0 until floatvar6(parnum)(i)(i2).length) {
                for (i4 <- 0 until floatvar6(parnum)(i)(i2)(i3).length) {
                  for (i5 <- 0 until floatvar6(parnum)(i)(i2)(i3).length) {
                    //                    rowList.add(RowFactory.create(dilist.get(0)(parnum), dilist.get(1)(i), dilist.get(2)(i2), dilist.get(3)(i3), dilist.get(4)(i4), dilist.get(5)(i5), floatvar6(parnum)(i)(i2)(i3)(i4)(i5)))

                    rowList = rowList :+ Row(dilist(0)(parnum), dilist(1)(i), dilist(2)(i2), dilist(3)(i3), dilist(4)(i4), dilist(5)(i5), floatvar6(parnum)(i)(i2)(i3)(i4)(i5))

                  }
                }
              }
            }
          }
          //根据2 dimension 分区
        }
        else for (i <- 0 until floatvar6(0)(parnum).length) {
          for (i2 <- 0 until floatvar6(0)(parnum)(i).length) {
            for (i3 <- 0 until floatvar6(0)(parnum)(i)(i2).length) {
              for (i4 <- 0 until floatvar6(0)(parnum)(i)(i2)(i3).length) {
                //                rowList.add(RowFactory.create(dilist.get(0)(0), dilist.get(1)(parnum), dilist.get(2)(i), dilist.get(3)(i2), dilist.get(4)(i3), dilist.get(5)(i4), floatvar6(0)(parnum)(i)(i2)(i3)(i4)))

                rowList = rowList :+ Row(dilist(0)(0), dilist(1)(parnum), dilist(2)(i), dilist(3)(i2), dilist(4)(i3), dilist(5)(i4), floatvar6(0)(parnum)(i)(i2)(i3)(i4))


              }
            }
          }
        }
        return rowList
      // case 7:
      //     float floatvar7[][][][][][][] = (float[][][][][][][]) variable.read().copyToNDJavaArray();
      //     // return new ArrayDouble.D6(index, storage);
      //     return rowList;
      case _ =>
        return null
    }
    catch {
      case e: Exception =>
        e.printStackTrace()
    }
    null
  }

  //
  //  def main(args: Array[String]): Unit = {
  //
  //
  //    for (i <- 0 until 1) {
  //
  //      println(i)
  //
  //    }
  //
  //
  //    val firdsleng = 9
  //    val firdslengflag = firdsleng > 1
  //
  //
  //    if (firdslengflag) {
  //
  //
  //      println("************")
  //    }
  //
  //  }

}
